package Day02_删除二叉搜索树中的节点;

/**
 * --------------------------------------------------------------------------------
 * 题目：
 *      给定一个二叉搜索树的根节点 root 和一个值 key，删除二叉搜索树中的key对应的节点，
 *      并保证二叉搜索树的性质不变。返回二叉搜索树（有可能被更新）的根节点的引用。
 *      一般来说，删除节点可分为两个步骤：首先找到需要删除的节点；如果找到了，删除它。
 * --------------------------------------------------------------------------------
 * 示例：
 *      输入：root = [5,3,6,2,4,null,7], key = 3
 *              5
 *            /   \
 *          3      6
 *        /  \      \
 *       2    4      7
 *      输出：[5,4,6,2,null,null,7]
 *      解释：给定需要删除的节点值是 3，所以我们首先找到 3 这个节点，然后删除它。
 *          一个正确的答案是 [5,4,6,2,null,null,7],
 *               5
 *             /  \
 *            2    6
 *             \    \
 *              4    7
 *          另一个正确答案是 [5,2,6,null,4,null,7]。
 *               5
 *             /  \
 *            4    6
 *             \    \
 *              2    7
 * --------------------------------------------------------------------------------
 * 方法：
 *      方法1：递归
 *      方法2：迭代
 * --------------------------------------------------------------------------------
 */
class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }

class Solution {
    public static void main(String[] args) {
        System.out.println(1);
    }
    public static TreeNode deleteNode1(TreeNode root, int key) {
        if (root == null) {
            return null;
        }
        if (root.val > key) {
            root.left = deleteNode1(root.left, key);
            return root;
        }
        if (root.val < key) {
            root.right = deleteNode1(root.right, key);
            return root;
        }
        if (root.val == key) {
            if (root.left == null && root.right == null) {
                return null;
            }
            if (root.right == null) {
                return root.left;
            }
            if (root.left == null) {
                return root.right;
            }
            TreeNode successor = root.right;
            while (successor.left != null) {
                successor = successor.left;
            }
            root.right = deleteNode1(root.right, successor.val);
            successor.right = root.right;
            successor.left = root.left;
            return successor;
        }
        return root;
    }
    public static TreeNode deleteNode2(TreeNode root, int key) {
        TreeNode cur = root, curParent = null;
        while (cur != null && cur.val != key) {
            curParent = cur;
            if (cur.val > key) {
                cur = cur.left;
            } else {
                cur = cur.right;
            }
        }
        if (cur == null) {
            return root;
        }
        if (cur.left == null && cur.right == null) {
            cur = null;
        } else if (cur.right == null) {
            cur = cur.left;
        } else if (cur.left == null) {
            cur = cur.right;
        } else {
            TreeNode successor = cur.right, successorParent = cur;
            while (successor.left != null) {
                successorParent = successor;
                successor = successor.left;
            }
            if (successorParent.val == cur.val) {
                successorParent.right = successor.right;
            } else {
                successorParent.left = successor.right;
            }
            successor.right = cur.right;
            successor.left = cur.left;
            cur = successor;
        }
        if (curParent == null) {
            return cur;
        } else {
            if (curParent.left != null && curParent.left.val == key) {
                curParent.left = cur;
            } else {
                curParent.right = cur;
            }
            return root;
        }
    }
}